//action
//读取设置
document.addEventListener("DOMContentLoaded",function(){
    getProxySetting().then(()=>{
		layui.use(async function(){
			console.debug("action");
			var actionSettings = settings;
			var actionFixedModeSettings = fixedModeSettings;
			var actionAdvancedFixedModeSettings = advancedFixedModeSettings;
			var actionIntelligentModeSettings = intelligentModeSettings;
			var form = layui.form;
			var $ = layui.$;

			var advancedFixedModeSettingsisEnable = actionAdvancedFixedModeSettings.isEnable ? "on" : "";
			//从Local 取出 设置
			form.val("baseSettings", {
				//模式选择
				"mode": actionSettings.mode,
				//pac脚本设置
				"pac_url": actionSettings.pac_url,
				//手动设置代理
				"singleProxySettingAddress": fixedModeSettings.singleProxySetting.host,
				"singleProxySettingPort": fixedModeSettings.singleProxySetting.port,
				"singleProxySettingScheme": fixedModeSettings.singleProxySetting.scheme,
				"singleProxySettingBypassList": fixedModeSettings.bypass_list,
				//手动设置代理-高级设置
				"advancedFixedModeSettingsisEnable" : advancedFixedModeSettingsisEnable,
				//HTTP
				"advancedFixedModeSettingsHTTPAddress" : actionAdvancedFixedModeSettings.proxyForHttpSetting.host,
				"advancedFixedModeSettingsHTTPPort" : actionAdvancedFixedModeSettings.proxyForHttpSetting.port,
				"advancedFixedModeSettingsHTTPScheme" : actionAdvancedFixedModeSettings.proxyForHttpSetting.scheme,
				//HTTPS
				"advancedFixedModeSettingsHTTPSAddress" : actionAdvancedFixedModeSettings.proxyForHttpsSetting.host,
				"advancedFixedModeSettingsHTTPSPort" : actionAdvancedFixedModeSettings.proxyForHttpsSetting.port,
				"advancedFixedModeSettingsHTTPSScheme" : actionAdvancedFixedModeSettings.proxyForHttpsSetting.scheme,
				//FTP
				"advancedFixedModeSettingsFTPAddress" : actionAdvancedFixedModeSettings.proxyForFtpSetting.host,
				"advancedFixedModeSettingsFTPPort" : actionAdvancedFixedModeSettings.proxyForFtpSetting.port,
				"advancedFixedModeSettingsFTPScheme" : actionAdvancedFixedModeSettings.proxyForFtpSetting.scheme,
				//其他协议
				"advancedFixedModeSettingsFallbackAddress" : actionAdvancedFixedModeSettings.fallbackProxySetting.host,
				"advancedFixedModeSettingsFallbackPort" : actionAdvancedFixedModeSettings.fallbackProxySetting.port,
				"advancedFixedModeSettingsFallbackScheme" : actionAdvancedFixedModeSettings.fallbackProxySetting.scheme,
				//绕行列表
				"advancedFixedModeSettingsBypassList" : actionAdvancedFixedModeSettings.advanced_fixed_mode_bypass_list
			});
			//保存所有设置
			var saveAll = async function(){
				var data = form.val('baseSettings');
				console.debug("function saveAll start data = ",data);
				//模式选择
				actionSettings.mode = data.mode;
				//pac脚本设置
				actionSettings.pac_url = data.pac_url;
				//手动设置代理
				fixedModeSettings.singleProxySetting.host = data.singleProxySettingAddress;
				fixedModeSettings.singleProxySetting.port = StringToInteger(data.singleProxySettingPort);
				fixedModeSettings.singleProxySetting.scheme = data.singleProxySettingScheme;
				fixedModeSettings.bypass_list = splitStringByDelimiter(data.singleProxySettingBypassList);
				//手动设置代理-高级设置
				actionAdvancedFixedModeSettings.isEnable = data.advancedFixedModeSettingsisEnable == "on" ? true : false;
				//HTTP
				actionAdvancedFixedModeSettings.proxyForHttpSetting.host = data.advancedFixedModeSettingsHTTPAddress;
				actionAdvancedFixedModeSettings.proxyForHttpSetting.port = StringToInteger(data.advancedFixedModeSettingsHTTPPort);
				actionAdvancedFixedModeSettings.proxyForHttpSetting.scheme = data.advancedFixedModeSettingsHTTPScheme;
				//HTTPS
				actionAdvancedFixedModeSettings.proxyForHttpsSetting.host = data.advancedFixedModeSettingsHTTPSAddress;
				actionAdvancedFixedModeSettings.proxyForHttpsSetting.port = StringToInteger(data.advancedFixedModeSettingsHTTPSPort);
				actionAdvancedFixedModeSettings.proxyForHttpsSetting.scheme = data.advancedFixedModeSettingsHTTPSScheme;
				//FTP
				actionAdvancedFixedModeSettings.proxyForFtpSetting.host = data.advancedFixedModeSettingsFTPAddress;
				actionAdvancedFixedModeSettings.proxyForFtpSetting.port = StringToInteger(data.advancedFixedModeSettingsFTPPort);
				actionAdvancedFixedModeSettings.proxyForFtpSetting.scheme = data.advancedFixedModeSettingsFTPScheme;
				//其他协议
				actionAdvancedFixedModeSettings.fallbackProxySetting.host = data.advancedFixedModeSettingsFallbackAddress;
				actionAdvancedFixedModeSettings.fallbackProxySetting.port = StringToInteger(data.advancedFixedModeSettingsFallbackPort);
				actionAdvancedFixedModeSettings.fallbackProxySetting.scheme = data.advancedFixedModeSettingsFallbackScheme;
				//绕行列表
				actionAdvancedFixedModeSettings.advanced_fixed_mode_bypass_list = splitStringByDelimiter(data.advancedFixedModeSettingsBypassList) ;
				await saveProxySetting();
				console.debug("function saveAll end.");
			};
			//保存设置
			layui.$('#saveAll').on('click',await saveAll);
			//连接并保存
			layui.$('#connect').on('click',await async function(){
				await saveAll();
				selectProxy(actionSettings.mode,null);
			});
			//重置设置
			layui.$('#resetAll').on('click',await async function(){
				await clearProxySetting();
				// 重新加载页面
				location.reload(true);
				
			});
			//跳转到智能模式设置页面
			layui.$('#intelligent_mode_setting').on('click',await async function(){
				chrome.tabs.create({ url: "../intelligent/intelligent.html" });
			});
			console.debug("action end");
		});
	});
});







